package org.gruposp2p.dnie.ui.panels;

import dniegp2pclient.DNIeGP2PClientApp;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
import org.gruposp2p.dnie.ui.dialogs.SignedDocumentDialog;
import org.gruposp2p.dnie.utils.FileUtils;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author jj
 */
public class SignatureResultPanel extends javax.swing.JPanel {

    private static Logger logger =
            LoggerFactory.getLogger(SignatureResultPanel.class);

    ResourceMap resourceMap = null;
    private String xmlSignedDocument;
    private String documentToSignTitle;

    public SignatureResultPanel() {
        initComponents();
        resourceMap = Application.getInstance(DNIeGP2PClientApp.class)
                .getContext().getResourceMap(SignatureResultPanel.class);
    }

    private void resetPanel () {
        detailsButton.setVisible(false);
        labelResult.setIcon(null);
        labelResult.setText("");
    }

    public void setSignatureOK (
            String xmlSignedDocument, String documentToSignTitle) {
        resetPanel ();
        this.xmlSignedDocument = xmlSignedDocument;
        this.documentToSignTitle = documentToSignTitle;
        detailsButton.setVisible(true);
        labelResult.setForeground(resourceMap.getColor("labelResult.foreground"));
        labelResult.setIcon(resourceMap.getIcon("labelResult_OK.icon"));
        labelResult.setText(resourceMap.getString("labelResult_OK.text"));
        labelResult.setVisible(true);
        this.setVisible(true);
        repaint();
    }

    public void setSignatureERROR () {
        resetPanel ();
        labelResult.setForeground(resourceMap.getColor("labelResult.foregroundERROR"));
        labelResult.setIcon(resourceMap.getIcon("labelResult_ERROR.icon"));
        labelResult.setText(resourceMap.getString("labelResult_ERROR.text"));
        this.setVisible(true);
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelResult = new javax.swing.JLabel();
        detailsButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        labelResult.setFont(new java.awt.Font("DejaVu Sans", 1, 15));
        labelResult.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/signature-ok_22x22.png"))); // NOI18N
        labelResult.setText("Firma correcta");
        labelResult.setName("labelResult"); // NOI18N

        detailsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/cert_16x16.png"))); // NOI18N
        detailsButton.setText("Detalles");
        detailsButton.setName("detailsButton"); // NOI18N
        detailsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detailsButtonguardarHandler(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(SignatureResultPanel.class, this);
        saveButton.setAction(actionMap.get("saveSignedDocument")); // NOI18N
        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/filesave_16x16.png"))); // NOI18N
        saveButton.setText("Guardar");
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelResult)
                .addGap(18, 18, 18)
                .addComponent(detailsButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(labelResult)
                .addComponent(detailsButton)
                .addComponent(saveButton))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void detailsButtonguardarHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detailsButtonguardarHandler
        if (xmlSignedDocument != null) {
            SignedDocumentDialog signedDocumentDialog =
                new SignedDocumentDialog(
                DNIeGP2PClientApp.getApplication().getView().getFrame(), true, xmlSignedDocument);
            signedDocumentDialog.setVisible(true);
        }
    }//GEN-LAST:event_detailsButtonguardarHandler

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        final JFileChooser chooser = new JFileChooser();
        chooser.addPropertyChangeListener(JFileChooser.DIRECTORY_CHANGED_PROPERTY,
            new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    chooser.setSelectedFile(
                    new File(chooser.getCurrentDirectory().getAbsolutePath() +
                    "\\" + documentToSignTitle));
                    chooser.updateUI();
                }
            });
         chooser.showSaveDialog(
                 DNIeGP2PClientApp.getApplication().getView().getFrame());
         File file = chooser.getSelectedFile();
         if (file!= null) {
             FileUtils.save( xmlSignedDocument,
                     file.getAbsolutePath(), "xml");
         }
    }//GEN-LAST:event_saveButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton detailsButton;
    private javax.swing.JLabel labelResult;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
